package models

import (
	"database/sql"
	"gorm.io/gorm"
	"time"
)

type User struct {
	//Id        int       `gorm:"column:id;primary_key" json:"id"`
	//Name      string    `gorm:"index;column:name;type:varchar(40)"`
	//Age       int64     `gorm:"column:age"`
	//Score     float64   `gorm:"column:score"`
	//CreatedAt time.Time `gorm:"column:created_at"`
	//UpdatedAt time.Time `gorm:"column:updated_at"`
	//
	////Belongs to
	//CompanyID int     `gorm:"column:company_id"`
	//Company   Company `gorm:"foreignKey:company_id;references:code"`
	//// 使用 CompanyID 作为外键
	//// 使用 Code 作为引用
	//
	//// Has One
	//CreditCard CreditCard `gorm:"foreignKey:username;references:name"`
	//// 使用 UserName 作为外键
	//
	//// Has Many
	//CreditCards []CreditCard `gorm:"foreignKey:username;references:name"`

	gorm.Model
	Birthday time.Time
	Age      int
	Name     string `gorm:"size:255"`       // string默认长度为255, 使用这种tag重设。
	Num      int    `gorm:"AUTO_INCREMENT"` // 自增

	CreditCard CreditCard `gorm:"foreignKey:UserID;references:ID"` // One-To-One (拥有一个 - CreditCard表的UserID作外键)
	Emails     []Email    `gorm:"foreignKey:UserID;references:ID"` // One-To-Many (拥有多个 - Email表的UserID作外键)

	BillingAddress   Address `gorm:"foreignKey:BillingAddressID;references:ID"` // One-To-One (属于 - 本表的BillingAddressID作外键)
	BillingAddressID sql.NullInt64

	ShippingAddress   Address `gorm:"foreignKey:ShippingAddressID;references:ID"` // One-To-One (属于 - 本表的ShippingAddressID作外键)
	ShippingAddressID int

	IgnoreMe  int        `gorm:"-"`                         // 忽略这个字段
	Languages []Language `gorm:"many2many:user_languages;"` // Many-To-Many , 'user_languages'是连接表

	Orders []Order `gorm:"foreignKey:UserID;references:ID"` // One-To-Many (拥有多个 - Order表的UserID作外键)
}

func (u *User) TableName() string {
	return "user"
}

/*type Company struct {
	ID   int    `gorm:"column:id;primary_key" json:"id"`
	Code int    `gorm:"column:code"`
	Name string `gorm:"column:name;type:varchar(40)"`
}

func (u *Company) TableName() string {
	return "company"
}*/

type Email struct {
	ID         int
	UserID     int    `gorm:"index"`                          // 外键 (属于), tag `index`是为该列创建索引
	Email      string `gorm:"type:varchar(100);unique_index"` // `type`设置sql类型, `unique_index` 为该列设置唯一索引
	Subscribed bool
}

func (u *Email) TableName() string {
	return "email"
}

type Address struct {
	ID       int
	Address1 string         `gorm:"not null;unique"` // 设置字段为非空并唯一
	Address2 string         `gorm:"type:varchar(100);unique"`
	Post     sql.NullString `gorm:"not null"`
}

func (u *Address) TableName() string {
	return "address"
}

type Language struct {
	ID   int
	Name string `gorm:"index:idx_name_code"` // 创建索引并命名，如果找到其他相同名称的索引则创建组合索引
	Code string `gorm:"index:idx_name_code"` // `unique_index` also works
}

func (u *Language) TableName() string {
	return "language"
}

type CreditCard struct {
	gorm.Model
	UserID uint   `gorm:"column:user_id"`
	Number string `gorm:"column:number;type:varchar(40)"`
}

func (u *CreditCard) TableName() string {
	return "credit_card"
}

type Order struct {
	gorm.Model
	UserID uint
	Price  float64
}

func (u *Order) TableName() string {
	return "order"
}
